import Cookies from 'js-cookie';
import { message } from 'antd';
import * as types from './actionTypes';
import { productPrice } from '@/api/order';

// 通过接口，获取订单价格/数量
export const setProductInfo = (params) => {
  // 若数量为undefined，则重新获取cookie
  if(params.amount === undefined){
    params.amount = Cookies.get('order_number') || 1;
  }
  const _amount = Number(params.amount);
  return dispatch => {
    _amount ? productPrice(params).then(res => {
      const { code, data } = res;
      if(code === 0 && data){
        const { saleAllPrice, reducePrice, price, amount, stock } = data;
        dispatch({
          type: types.SET_PRODUCT_INFO,
          salePrice: saleAllPrice || 0,
          reducePrice: reducePrice || 0,
          price: price || 0,
          amount: amount || _amount || 0,
          stock,
        })
      }else{
        message.error('获取订单信息失败，请重试')
      }
    }) : dispatch({
          type: types.SET_PRODUCT_INFO,
          salePrice: 0,
          reducePrice: 0,
          price: 0,
          amount: 0,
        })
  }
}
export const setProductInfo_0 = () => {
  return dispatch => {
    dispatch({
      type: types.SET_PRODUCT_INFO,
      salePrice: 0,
      reducePrice: 0,
      price: 0,
      amount: 0,
    })
  }
}
// 选择日期，单选
export const setSelectDate = (date) => {
  return dispatch => {
    dispatch({
      type: types.SET_SELECT_DATE,
      date
    })
  }
}
// 开始、结束日期
export const setSelectDateRange = ({ startDate, endDate }) => {
  return dispatch => {
    dispatch({
      type: types.SET_SELECT_DATE_RANGE,
      startDate,
      endDate
    })
  }
}